package com.sgb.auction.activity;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.sgb.auction.R;
import com.sgb.auction.adapter.BiddingQueryAdapter;
import com.sgb.auction.databinding.ActivityBiddingQueryBinding;
import com.sgb.auction.entity.BiddingQueryListEntity;
import com.sgb.auction.entity.RequestBean;
import com.sgb.auction.viewmodule.BiddingQueryModel;
import com.swgk.core.base.AppActivity;
import com.swgk.core.view.selection.CommonPopEntity;
import com.swgk.core.view.selection.MemberStatusView;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

/**
 * Time:2021/11/19
 * AUTHOR:F_Q
 * Description:竞买查询
 */
public class BiddingQueryActivity  extends AppActivity implements MemberStatusView.OnChooseListener{
    private RequestBean requestBean = new RequestBean();
    private ActivityBiddingQueryBinding mBinding;
    private BiddingQueryModel mModel;
    private boolean isLoadMore = false;
    private BiddingQueryAdapter biddingqueryadapter;
    private String bidding = "";

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, BiddingQueryActivity.class);
        context.startActivity(intent);
    }
    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_bidding_query);
        mModel = new ViewModelProvider(this).get(BiddingQueryModel.class);
        mModel.setActivity(BiddingQueryActivity.this);
        mBinding.includeTitle.ivBack.setOnClickListener(v -> finish());
        mBinding.includeTitle.titleView.setText("竞买查询");
        if (biddingqueryadapter == null) {
            biddingqueryadapter = new BiddingQueryAdapter(getActivity(), null);
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        mBinding.recyclerView.setLayoutManager(layoutManager);
        mBinding.recyclerView.setHasFixedSize(true);
        //解决数据加载完成后, 没有停留在顶部的问题
        mBinding.recyclerView.setFocusable(false);
        mBinding.recyclerView.setAdapter(biddingqueryadapter);
        mBinding.recyclerView.setLoadingMoreEnabled(true);
        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initDataList();
            }

            @Override
            public void onLoadMore() {
                isLoadMore = true;
                requestBean.page++;
                //上拉加载更多
                mModel.BiddingQueryList(requestBean);
            }
        });
        biddingqueryadapter.setOnItemProcessMode(new BiddingQueryAdapter.onItemProcessMode() {
            @Override
            public void onAuctionDetails(BiddingQueryListEntity biddingQueryListEntity) {
                //拍卖详情
                mModel.getWebView(biddingQueryListEntity.getNo());
            }

            @Override
            public void onBiddingRecord(BiddingQueryListEntity biddingQueryListEntity) {
                //竞买记录
                BiddingRecordActivity.start(getActivity(),biddingQueryListEntity.getName(),biddingQueryListEntity.getNo());

            }
        });
        //搜索
        mBinding.tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(mBinding.editSearch.getText().toString().trim())) {
                    requestBean.setName(mBinding.editSearch.getText().toString().trim());
                    initDataList();
                } else {
                    requestBean.setName("");
                    initDataList();
                }
            }
        });
        mBinding.editSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 1) {
                    mBinding.searchDelete.setVisibility(View.VISIBLE);
                } else {
                    mBinding.searchDelete.setVisibility(View.GONE);
                }
            }
        });
        mBinding.searchDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mBinding.editSearch.setText("");
                mBinding.searchDelete.setVisibility(View.GONE);
                requestBean.setName("");
                initDataList();
            }
        });
        //初始化popview
        initializePopView();
    }
    private void initializePopView() {
        //竞买状态
        mBinding.biddingState.initPopup(mBinding.grayLayout, mBinding.popLine, this);
    }


    @Override
    protected void onResume() {
        super.onResume();
        initDataList();
    }

    private void initDataList() {
        isLoadMore = false;
        requestBean.page = 1;
        mModel.BiddingQueryList(requestBean);
    }

    public void setBiddingQueryList(List<BiddingQueryListEntity> data, boolean lastPage) {
        RecyclerRefreshing();
        if (isLoadMore) {
            if (data != null && data.size() > 0) {
                biddingqueryadapter.addDatas(data);
            }
            mBinding.recyclerView.setNoMore(lastPage);
        } else {
            if (data != null && data.size() > 0) {
                mBinding.emptyView.setVisibility(View.GONE);
                mBinding.recyclerView.setVisibility(View.VISIBLE);
                biddingqueryadapter.setDatas(data);
                mBinding.recyclerView.scrollToPosition(0);
                mBinding.recyclerView.setNoMore(lastPage);
            } else {
                biddingqueryadapter.clearDatas();
                mBinding.emptyView.setVisibility(View.VISIBLE);
                mBinding.recyclerView.setVisibility(View.GONE);
            }
        }
    }

    private void RecyclerRefreshing() {
        mBinding.recyclerView.refreshComplete();
        mBinding.recyclerView.loadMoreComplete();
    }

    @Override
    protected void init() {
        super.init();
        mModel.getBiddingState();
    }

    //pop初始化数据
    @Override
    public void onLoadChooseData(String mKeyName) {
        switch (mKeyName) {
            case "竞买状态":
                mModel.getBiddingState();
                break;
        }
    }

    //pop点击事件
    @Override
    public void onChooseResult(String mKeyName, CommonPopEntity entity, Object... objects) {
        int position = (int) objects[0];
        String no = position == 0 ? "" : entity.getNo();
        String id = position == 0 ? "" : entity.getId();
        String name = position == 0 ? "" : entity.getTypeName();
        switch (mKeyName) {
            case "竞买状态":
                if (!TextUtils.equals(no, bidding)) {
                    bidding = no;
                    requestBean.setState(bidding);
                }
                break;
        }
        //刷新数据
        initDataList();
    }

    //弹窗信息
    public void setChooseItemData(int type, List<CommonPopEntity> data) {
        if (data == null || data.size() == 0) return;
        switch (type) {
            case 1://竞买状态
                mBinding.biddingState.setData(data, bidding);
                break;
        }
    }

    /**
     * 竞买状态返回
     *
     * @param data
     */
    public void setBiddingStateList(List<CommonPopEntity> data) {
        setChooseItemData(1, data);
    }
}
